<?php
namespace App\Http\Controllers\Api\User;
use App\Http\Controllers\ApiController;
use App\Http\Controllers\Controller;
use App\Http\Models\User\UserBaseModel;
use App\Models\distribution;
use App\Models\Hotel;
use App\Models\HotelOwner;
use App\Models\Order;
use App\Models\rebate;
use App\Models\RechargeCommission;
use App\Models\RechargeOrder;
use App\Models\SystemConfig;
use App\Models\Vip;
use Illuminate\Http\Request;
use App\Models\CopartnerApply;
class ReBateController extends ApiController
{
    #商品订单和充值分佣:根据uid给上级 分佣
    public function RebateByUid($uid,$order_sn,$total_money,$hotel_id)
    {
        #获得酒店分成比例
        $divide_rate = Hotel::find($hotel_id) -> divide_rate ;
        $hotelOwner = HotelOwner::getOwnerByHotelId($hotel_id);
        if(empty($hotelOwner)){
            return false;
        }
        $data = [
            'buy_uid' => $uid,
            'beneficiary_uid' => $hotelOwner -> hotel_owner_id ,
            'order_an' => $order_sn,
            'order_money' => $total_money,
            'rate' => $divide_rate,
            'level' => 1,
            'created_at' => time(),
            'updated_at' => time(),
            'hotel_id' => $hotel_id
        ];
        $flag   = distribution::addDistributionRecord($data);
        return $flag;
    }


    #代充值 分佣
    public function rechargeCommissionByUid(Request $request)
    {
        $input = $request->all();
        if (empty($input['from_uid'])){
            return self::response(null,self::ERROR_CODE,'from_uid null');
        }
        if (empty($input['total_amount'])){
            return self::response(null,self::ERROR_CODE,'请填写充值金额');
        }

        if (empty($input['pay_channel'])||!in_array($input['pay_channel'],[1,2,3])){
            return self::response(null,self::ERROR_CODE,'支付方式错误');
        }

        if (empty($input['type'])){
            return self::response(null,self::ERROR_CODE,'充值方式不能为空');
        }

        $partner = CopartnerApply::getPartnerInfoByUserId($input['from_uid']);
        if (empty($partner)){
            return self::response(null,self::ERROR_CODE,'您不是合伙人，不能进行此操作！');
        }

        $input['order_sn']                      = ApiController::get_order_sn(3);
        if ($input['type']==1){
            if (empty($input['to_user_name'])){
                return self::response(null,self::ERROR_CODE,'请填写充值用户名');
            }
            $to_user_info                       = UserBaseModel::getUserBaseInfo($input['to_user_name']);
        }else if ($input['type'] == 2){
            if (empty($input['vip_cared_sn'])){
                return self::response(null,self::ERROR_CODE,'会员卡卡号');
            }
            $to_user_info                       = Vip::getInfoByVipSn($input['vip_cared_sn']);
        }
        if (empty($to_user_info['id'])){
            return self::response(null,self::ERROR_CODE,'充值用户不存在');
        }
        $input['to_uid']                        = $to_user_info['id'];
        if(empty($to_user_info['open_id'])){
            return self::response(null,self::UN_BIND_OPENID,'未绑定open_id');
        }



        #生成充值订单订单
        $Id                                   = RechargeOrder::addRechargeRebateOrder($input);
        if($Id>0){
            $order = RechargeOrder::find($Id);
            return  self::response(['order'=>$order],200);
        }else{
            return self::response(null,-5,'操作失败');
        }
    }


    public function getRebateResult($uid = 0,$total_amount)
    {
        $rebate             = [];
        #获取各个分佣等级的分佣比例
        $rateList           = rebate::getRateList();
        for ($i=1;$i<=3;$i++){
            $key            = 'BNF_Lv'.$i;
            $rebate[$key]   =  $rateList[$key] * $total_amount;
        }
        return $rebate;
    }
}
